// pages/productDetail/productDetail.js
const shopping=require("../../utils/shopping.js")
const eventBus = require('../../utils/eventbus.js');
Page({
  pid:"",
  data: {
    detail:null,
    cartItemCount:0
  },

  onLoad: function (options) {
   let self=this
   let pid=options.pid
   this.pid=pid
   wx.request({
     url: 'http://localhost:3000/api/getdetail?pid='+pid,
     dataType: 'json',
     success: function(res) {
       self.setData({
         detail: res.data.productDetail,
       })
      //  console.log(self.data.detail)
     },
   })
    this.setData({
      cartItemCount: shopping.getCount()
    })
    // event bus 监听一个事件 refreshCartCount
    eventBus.on('refreshCartCount', (data) => {
      this.setData({
        cartItemCount: shopping.getCount()
      })
    })
  },
  addtoShop(){
    shopping.addShopping(this.pid)
    wx.navigateTo({
      url: '../shoppingCar/shoppingCar',
    })
  }
})
